package cn.snowtech.bluebox.service;

import cn.snowtech.bluebox.domain.Medicine;
import cn.snowtech.bluebox.domain.Result;
import cn.snowtech.bluebox.dto.medicine.*;
import cn.snowtech.bluebox.repository.MedicineRepository;
import cn.snowtech.bluebox.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MedicineService {
    @Autowired
    MedicineRepository medicineRepository;
    public MedicineResponseDTO create(MedicineRequestDTO requestDTO){

        Medicine medicine=new Medicine();
        medicine.setName(requestDTO.getName());
        medicine.setRemindDate(requestDTO.getRemindDate());
        medicine.setMemo(requestDTO.getMemo());
        if(requestDTO.getRemindDate().compareTo(DateUtil.getCurrentDate())<0){
            medicine.setOverdure(1);
        }else {
            medicine.setOverdure(0);
        }
        medicine=medicineRepository.save(medicine);

        MedicineResponseDTO responseDTO=new MedicineResponseDTO();
        responseDTO.setResult(Result.R1000.getCode());
        responseDTO.setMessage(Result.R1000.getMessage());
        return responseDTO;
    }

    public MedicineQueryResponseDTO getMedicines(MedicineQueryRequestDTO requestDTO){
        List<Medicine> medicineList=new ArrayList<>();
        if(requestDTO.getCatalog().equals(2)) {
            medicineList= medicineRepository.findAll();
        }else {
            medicineList= medicineRepository.findByOverdure(requestDTO.getCatalog());
        }

        MedicineQueryResponseDTO responseDTO=new MedicineQueryResponseDTO();
        responseDTO.setResult(Result.R1000.getCode());
        responseDTO.setMessage(Result.R1000.getMessage());

        List<MedicineDetailDTO> detailDTOList=new ArrayList<>();
        for(Medicine medicine:medicineList){
            MedicineDetailDTO detailDTO=new MedicineDetailDTO();
            detailDTO.setName(medicine.getName());
            detailDTO.setMemo(medicine.getMemo());
            detailDTO.setOverdure(medicine.getOverdure());
            detailDTO.setRemindDate(medicine.getRemindDate());
            detailDTOList.add(detailDTO);
        }
        responseDTO.setMedicineDetailDTOS(detailDTOList);

        return responseDTO;
    }
    public MedicineQueryResponseDTO searchMedicines(MedicineSearchRequestDTO requestDTO){
        List<Medicine> medicineList= medicineRepository.findByNameLike(requestDTO.getName());
        MedicineQueryResponseDTO responseDTO=new MedicineQueryResponseDTO();
        responseDTO.setResult(Result.R1000.getCode());
        responseDTO.setMessage(Result.R1000.getMessage());

        List<MedicineDetailDTO> detailDTOList=new ArrayList<>();
        for(Medicine medicine:medicineList){
            MedicineDetailDTO detailDTO=new MedicineDetailDTO();
            detailDTO.setName(medicine.getName());
            detailDTO.setMemo(medicine.getMemo());
            detailDTO.setOverdure(medicine.getOverdure());
            detailDTO.setRemindDate(medicine.getRemindDate());
            detailDTOList.add(detailDTO);
        }
        responseDTO.setMedicineDetailDTOS(detailDTOList);

        return responseDTO;
    }
}
